<?php

namespace App\GameChannel\Sdks;

use App\GameChannel\LoginNotify;
use App\GameChannel\PayNotify;
use App\GameChannel\SdkBase;
use App\Models\Orders;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class LingchuangSdk extends SdkBase
{
    public function login(Request $request): LoginNotify
    {
        $notify = $this->loginNotify;
        $params = $request->all();

        $token     = $params['access_token'];
        $user_id   = $params['openid'];
        $timestamp = $params['extend_2'];
        $loginkey  = $this->gameChannel['gamepublickey'];

        $check_sign = strtolower(md5("{$loginkey}{$user_id}{$timestamp}"));

        if ($check_sign != $token) {
            // $notify->errno = 1;
            // return $notify;
        }

        $notify->openid = $user_id;
        return $notify;
    }

    // 处理充值回调
    public function payCb(Request $request, $cb_type): PayNotify
    {
        $notify = $this->payNotify;
        $params = $request->all();

        // 校验签名
        $params['payKey'] = $this->gameChannel['paykey'];
        $check_sign = '';
        foreach (['gameCode','serverCode','payKey','timestamp','price','productId','orderId'] as $key) {
            $check_sign .= $params[$key];
        }
        $check_sign = hash('sha256', $check_sign);
        if ($check_sign !== $params['accessToken']) {
            $notify->errno = 101;
            return $notify;
        }

        // 校验订单
        $trade_no = $params['cpOrderId'];
        $order    = Orders::where('trade_no', $trade_no)->first();
        if (!$order) {
            $notify->errno = 6011;
            return $notify;
        }

        // 如果此订单已经被处理过
        if ($order->status != Orders::PAY_STATUS_CREATE) {
            $notify->errno = 6012;
            return $notify;
        }

        // 校验充值金额(美金)
        if (abs($order->amount - $params['price']) > 1) {
            $notify->errno = 6013;
            return $notify;
        }

        // 校验用户
        if ($order->channel_openid != $params['userId']) {
            $notify->errno = 6014;
            return $notify;
        }

        $notify->order          = $order;
        $notify->tradeNo        = $trade_no;
        $notify->channelTradeNo = $params['orderId'];
        $notify->isTest         = $params['isTest'] == 1 ? 1 : 0;
        $notify->extendData     = $params['payFrom'] ?? '';
        return $notify;
    }

    // 支付成功补齐订单其他信息
    public function supplementOrderInfo()
    {
        $notify = $this->payNotify;
        $order = $notify->order;
        $order->is_test = $notify->isTest;
        $order->extend_1 = $notify->extendData;

        return true;
    }

    public function payOk()
    {
        return json_encode(['code' => 1000, 'message'=> '成功']);
    }

    public function payErr()
    {
        if ($this->payNotify->errno == 6012) {
            return json_encode(['code' => 1000, 'message'=> '成功']);
        }

        return json_encode(['code' => 1001, 'message'=> $this->payNotify->errno]);
    }
}
